package com.tianbing.adlottery.controller.user;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.validation.BindException;
import org.springframework.web.bind.ServletRequestUtils;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.SimpleFormController;

import com.fengniu.ejb.business.ICouponBusiness;
import com.fengniu.ejb.dao.ICouponDao;
import com.fengniu.ejb.entity.CouponItem;
import com.fengniu.ejb.entity.Site2Content;
import com.tianbing.adlottery.helper.SkinUtils;
import com.tianbing.adlottery.util.ControllerFormBean;
import com.tianbing.adlottery.util.UserSession;

public class EditMyCouponItemController extends SimpleFormController {
	static Logger logger = Logger.getLogger(EditMyCouponItemController.class.getName());

	private ICouponDao couponDao;

	private ICouponBusiness couponBusiness;

	private SkinUtils skinUtils;

	// ------------------GET Request LifeCycle------------------
	protected Object formBackingObject(HttpServletRequest request) throws Exception {
		UserSession auth = skinUtils.getUserAuthorization(request, null);
		ControllerFormBean fb = new ControllerFormBean();
		fb.setCouponItem(null);
		long cisid = ServletRequestUtils.getLongParameter(request, "cisid", -1);
		if (cisid > -1) {
			CouponItem ci = couponDao.findCouponItemBySysId(cisid);
			if (ci.getUser().getSysId().compareTo(auth.getUserSysId()) == 0) {
				fb.setCouponItem(ci);
			}
		}
		return fb;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.springframework.web.servlet.mvc.SimpleFormController#onSubmit(javax.servlet.http.HttpServletRequest,
	 *      javax.servlet.http.HttpServletResponse, java.lang.Object,
	 *      org.springframework.validation.BindException)
	 */
	protected ModelAndView onSubmit(HttpServletRequest request, HttpServletResponse response, Object cmd,
			BindException ex) throws Exception {

		ModelAndView mv = new ModelAndView(this.getSuccessView());
		ControllerFormBean form = (ControllerFormBean) cmd;
		UserSession auth = (UserSession) skinUtils.getUserAuthorization(request, response, true);
		CouponItem ci = form.getCouponItem();
		if (ci != null) {
			couponBusiness.updateCouponItemBonusNumber(auth.getUserSysId(), ci);
		}

		return mv;
	}

	protected Map referenceData(HttpServletRequest request) throws Exception {
		Map model = new HashMap();

		long cisid = ServletRequestUtils.getLongParameter(request, "cisid", -1);
		if (cisid > -1) {
			Site2Content store2Coupon = couponBusiness.findCouponStoreByCouponItemSysId(cisid);
			model.put("store2Coupon", store2Coupon);
		}
		return model;
	}

	public void setSkinUtils(SkinUtils skinUtils) {
		this.skinUtils = skinUtils;
	}

	public static void setLogger(Logger logger) {
		EditMyCouponItemController.logger = logger;
	}

	public void setCouponDao(ICouponDao couponDao) {
		this.couponDao = couponDao;
	}

	public void setCouponBusiness(ICouponBusiness couponBusiness) {
		this.couponBusiness = couponBusiness;
	}

}
